package com.yunhe.abnormal.controller.data;

import com.yunhe.abnormal.domain.data.AlarmEvent;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.abnormal.model.AlarmEventSearchDTO;
import com.yunhe.common.model.DomainList;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.data.AlarmEventDTO;
import com.yunhe.abnormal.service.data.AlarmEventService;
import com.yunhe.common.util.HeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 告警事件Controller
 * @desc 告警事件控制类定义
 * @author duanxiaoyong
 */
@Api(value = "alarmEvent",description = "事件维护", tags = {"Data_AlarmEvent"})
@RestController
@RequestMapping("/alarmEvents")
public class AlarmEventController {

    @Autowired
    private AlarmEventService eventService;
    @Autowired
    private HeaderUtil headerUtil;

    @ApiOperation("新增事件")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> post(@RequestBody AlarmEvent event)
            throws Exception {
        event.setId(null);
        AlarmEventDTO newEvent = eventService.createEvent(event);
        return new ResponseEntity<>(new ResultObject<>(newEvent), HttpStatus.OK);
    }

    /**
     * 批量新增事件
     * @param domainList 事件对象列表
     */
    @ApiOperation("批量新增事件")
    @RequestMapping(value = "/batch", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> batchPost(@RequestBody DomainList<AlarmEvent> domainList)
            throws Exception {
        List<AlarmEvent> EventList = domainList.getList();
        Iterable<AlarmEventDTO> newEvents = eventService.createEvent(EventList);
        return new ResponseEntity<>(new ResultObject<>(newEvents), HttpStatus.OK);
    }

    /**
     * 根据事件ID删除事件
     * @param id 事件ID
     */
    @ApiOperation("根据事件ID删除事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "事件ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
        eventService.deleteEventById(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("根据事件ID删除事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "事件ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/batch", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@RequestParam("ids") List<Long> id) {
        eventService.deleteEventById(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 根据电站ID删除相关事件
     * @param stationId 电站ID
     */
    @ApiOperation("根据电站ID删除事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "电站ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/byStationId", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> deleteByStationId(@RequestParam("stationId") Long stationId) {
        eventService.deleteEventByStationId(stationId);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 根据电站ID和时间范围删除相关事件
     * @param stationId 电站ID
     */
    @ApiOperation("根据电站ID删除事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "电站ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围", required = true, dataType = "date", paramType = "query")
    })
    @RequestMapping(value = "/byStationIdAndDtime", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> deleteByStationId(@RequestParam("stationId") Long stationId,
                                                          @RequestParam("dtime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") List<Date> dtime) throws ArgumentErrorException {
        eventService.deleteEventByStationId(stationId, dtime);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 更新事件（更新全部字段）
     * @param event 事件对象
     */
    @ApiOperation("更新事件（更新全部字段）")
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> put(@RequestBody AlarmEvent event)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AlarmEventDTO newEvent = eventService.updateEvent(event);
        return new ResponseEntity<>(new ResultObject<>(newEvent), HttpStatus.OK);
    }

    @ApiOperation("更新事件（更新指定字段）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "事件ID", required = true, dataType="long", paramType = "path"),
            @ApiImplicitParam(name = "firmId", value = "单位ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "firmTitle", value = "单位名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "stationId", value = "站点ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "stationTitle", value = "站点名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "devId", value = "设备ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "devTitle", value = "设备名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "alarmId", value = "异常名称ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmTitle", value = "异常名称名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "alarmTypeId", value = "异常类型ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmTypeTitle", value = "异常类型名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "abnormalStatusId", value = "异常状态ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "abnormalStatusTitle", value = "异常状态名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userIdProcess", value = "处理人id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "userTitleProcess", value = "处理人姓名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "continueTime", value = "持续时间", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间，如“2017-12-06 00:00:00”", dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "latestProcessTime", value = "处理时间，如“2017-12-06 00:00:00”", dataType = "date", paramType = "query")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public ResponseEntity<ResultObject> patch(@PathVariable("id") Long id,
                                              @RequestParam("firmId") Optional<Long> firmId,
                                              @RequestParam("firmTitle") Optional<String> firmTitle,
                                              @RequestParam("stationId") Optional<Long> stationId,
                                              @RequestParam("stationTitle") Optional<String> stationTitle,
                                              @RequestParam("devId") Optional<Long> devId,
                                              @RequestParam("devTitle") Optional<String> devTitle,
                                              @RequestParam("alarmId") Optional<Long> alarmStatusId,
                                              @RequestParam("alarmTitle") Optional<String> alarmStatusTitle,
                                              @RequestParam("alarmTypeId") Optional<Long> alarmTypeId,
                                              @RequestParam("alarmTypeTitle") Optional<String> alarmTypeTitle,
                                              @RequestParam("abnormalStatusId") Optional<Long> abnormalStatusId,
                                              @RequestParam("abnormalStatusTitle") Optional<String> abnormalStatusTitle,
                                              @RequestParam("userIdProcess") Optional<Long> userIdProcess,
                                              @RequestParam("userTitleProcess") Optional<String> userTitleProcess,
                                              @RequestParam("continueTime") Optional<Long> continueTime,
                                              @RequestParam("startTime") Optional<String> startTime,
                                              @RequestParam("latestProcessTime") Optional<String> latestProcessTime
    )
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AlarmEventDTO newEvent = eventService.updateEvent(id, firmId, firmTitle, stationId, stationTitle, devId, devTitle, alarmStatusId, alarmStatusTitle, alarmTypeId, alarmTypeTitle,
                abnormalStatusId, abnormalStatusTitle, userIdProcess, userTitleProcess, continueTime, startTime, latestProcessTime);
        return new ResponseEntity<>(new ResultObject<>(newEvent), HttpStatus.OK);
    }

    /**
     * 根据事件ID获得事件
     * @param id 事件号
     */
    @ApiOperation("根据事件ID获得事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "事件ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@PathVariable("id") Long id) {
        AlarmEventDTO event = eventService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(event), HttpStatus.OK);
    }

    @ApiOperation("根据设备ID，异常类型和异常状态获得事件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "devId", value = "设备ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmTypeId", value = "异常类型", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "abnormalStatusId", value = "告警状态", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search(@RequestParam("devId") Long devId,
                                               @RequestParam("alarmTypeId") Long alarmTypeId,
                                               @RequestParam("abnormalStatusId") Long abnormalStatusId) {
        Iterable<AlarmEventDTO> results = eventService.findEvents(devId, alarmTypeId, abnormalStatusId);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    @ApiOperation("根据设备ID和异常状态获得事件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "devId", value = "设备ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "abnormalStatusId", value = "异常状态", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/findByAbnormalStatusId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search(@RequestParam("devId") Long devId,
                                               @RequestParam("abnormalStatusId") Long abnormalStatusId) {
        AlarmEventDTO result = eventService.findEvents(devId, abnormalStatusId);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("获得全部正在告警事件")
    @RequestMapping(value = "/findAllWarning", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchAll() {
        Iterable<AlarmEventDTO> result = eventService.findAllWarning();
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据电站ID获得正在告警")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "电站Id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "queryStr", value = "查询条件", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/findAllWarning/station", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findWarningByStationId(@RequestParam("stationId") Long stationId,
                                                               @ModelAttribute PageParam pageParam) throws ArgumentErrorException {
        Iterable<AlarmEvent> results = eventService.findWarningByStationId(stationId, pageParam);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    @ApiOperation("根据条件获得事件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firmId", value = "单位ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "stationIds", value = "电站ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "devId", value = "设备ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmId", value = "异常名称", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmTypeId", value = "异常类型", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "abnormalStatusIds", value = "异常状态", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelIds", value = "告警级别", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "queryStr", value = "查询条件", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/findByCondition", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findByCondition(@RequestParam("firmId") Optional<Long> firmId,
                                                        @RequestParam("stationIds") Optional<List<Long>> stationIds,
                                                        @RequestParam("devId") Optional<Long> devId,
                                                        @RequestParam("alarmId") Optional<Long> alarmId,
                                                        @RequestParam("alarmTypeId") Optional<Long> alarmTypeId,
                                                        @RequestParam("abnormalStatusIds") Optional<List<Long>> abnormalStatusIds,
                                                        @RequestParam("alarmLevelIds") Optional<List<Long>> alarmLevelIds,
                                                        @RequestParam("dtime") Optional<String> dtime,
                                                        @RequestParam("queryStr") Optional<String> queryStr,
                                                        @ModelAttribute PageParam pageParam) {
        Iterable<AlarmEvent> results = eventService.findByCondition(firmId.orElse(null), stationIds.orElse(null),
                devId.orElse(null), alarmId.orElse(null), alarmTypeId.orElse(null), abnormalStatusIds.orElse(null),
                alarmLevelIds.orElse(null), dtime.orElse(null), queryStr.orElse(null), pageParam);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    @ApiOperation("根据时间范围（天）获取告警次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationIds", value = "电站ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "devId", value = "设备ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "abnormalStatusId", value = "异常状态ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmStatusId", value = "异常名称ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelId", value = "告警等级ID", dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/searchDay", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchDay(@RequestParam("stationIds") Optional<List<Long>> stationIds,
                                                  @RequestParam("devId") Optional<Long> devId,
                                                  @RequestParam("abnormalStatusId") Optional<Long> abnormalStatusId,
                                                  @RequestParam("alarmStatusId") Optional<Long> alarmStatusId,
                                                  @RequestParam("alarmLevelId") Optional<Long> alarmLevelId,
                                                  @RequestParam("dtime") List<String> dtime) {
        Map<String, Integer> result = eventService.searchDay(stationIds, devId, abnormalStatusId, alarmStatusId, alarmLevelId, dtime);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据时间范围（月）获取告警次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationIds", value = "电站ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "devId", value = "设备ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "abnormalStatusId", value = "异常状态ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelId", value = "告警等级ID", dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/searchMonth", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchMonth(@RequestParam("stationIds") Optional<List<Long>> stationIds,
                                                    @RequestParam("devId") Optional<Long> devId,
                                                    @RequestParam("abnormalStatusId") Optional<Long> abnormalStatusId,
                                                    @RequestParam("alarmLevelId") Optional<Long> alarmLevelId,
                                                    @RequestParam("dtime") List<String> dtime) {
        Map<String, Integer> result = eventService.searchMonth(stationIds, devId, abnormalStatusId, alarmLevelId, dtime);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据时间范围（年）获取告警次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationIds", value = "电站ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "devId", value = "设备ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "abnormalStatusId", value = "异常状态ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelId", value = "告警等级ID", dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/searchYear", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchYear(@RequestParam("stationIds") Optional<List<Long>> stationIds,
                                                    @RequestParam("devId") Optional<Long> devId,
                                                    @RequestParam("abnormalStatusId") Optional<Long> abnormalStatusId,
                                                    @RequestParam("alarmLevelId") Optional<Long> alarmLevelId,
                                                    @RequestParam("dtime") List<String> dtime) {
        Map<String, Integer> result = eventService.searchYear(stationIds, devId, abnormalStatusId, alarmLevelId, dtime);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据时间范围（天）获取告警次数最多的设备")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationIds", value = "电站ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelIds", value = "告警等级ID列表", required = false, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/searchTopDevice", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchTopDevice(@RequestParam("stationIds") Optional<List<Long>> stationIds,
                                                        @RequestParam("dtime") List<String> dtime,
                                                        @RequestParam("alarmLevelIds") Optional<List<Long>> alarmLevelIds) {
        Map<String, Integer> result = eventService.searchTopDevice(stationIds, dtime, alarmLevelIds.orElse(null));
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据时间范围（天）获取告警次数最多的告警名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationIds", value = "电站ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围", required = true, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelIds", value = "告警等级ID列表", required = false, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/searchTopAlarmStatus", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchTopAlarmStatus(@RequestParam("stationIds") Optional<List<Long>> stationIds,
                                                             @RequestParam("dtime") List<String> dtime,
                                                             @RequestParam("alarmLevelIds") Optional<List<Long>> alarmLevelIds) {
        Map<String, Integer> result = eventService.searchTopAlarmStatus(stationIds, dtime, alarmLevelIds.orElse(null));
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("获取最近一次添加的事件")
    @RequestMapping(value = "/findLastEvent", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findLastEvent() {
        AlarmEventDTO result = eventService.findLastEvent();
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * 根据异常名称查找事件
     * @param alarmStatusId 异常名称
     */
    @ApiOperation("根据异常名称查找事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "alarmStatusId", value = "异常名称ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchByFirmAndAlarmStatus(@RequestParam("alarmStatusId") Long alarmStatusId) {
        Long firmId = headerUtil.getHeaderFirmId();
        Iterable<AlarmEventDTO> results = eventService.search(firmId, alarmStatusId);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    @ApiOperation("根据时间获取事件数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dtime", value = "时间范围", required = true, dataType = "date", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelId", value = "告警等级ID", dataType = "long", paramType = "query")

    })
    @RequestMapping(value = "/searchAmount", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchAmount(@RequestParam("stationId") Long stationId,
                                                     @RequestParam("alarmLevelId") Optional<Long> alarmLevelId,
                                                     @RequestParam("dtime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") List<Date> dtime) {
        Map<String, Integer> result = eventService.searchAmount(stationId, alarmLevelId, dtime);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据时间获取事件数量_按能量单元分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "电站ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "dtime", value = "时间范围", required = true, dataType = "date", paramType = "query")
    })
    @RequestMapping(value = "/searchAmount/groupByEnergyUnit", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> amountGroupByEnergy(@RequestParam("stationId") Long stationId,
                                                            @RequestParam("dtime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") List<Date> dtime) {
        Map<Long, Integer> result = eventService.amountGroupByEnergyUnit(stationId, dtime);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

//    @ApiOperation("查找所有调式中产生的告警")
//    @RequestMapping(value = "/searchDebugging", method = RequestMethod.GET)
//    public ResponseEntity<ResultObject> searchDebugging() {
//        Iterable<AlarmEventDTO> result = eventService.findDebuggingEvent();
//        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
//    }

    /**
     * 获取新增或者状态发生改变的事件
     */
    @ApiOperation("获取新增或者状态发生改变的事件")
    @RequestMapping(value = "/searchChange", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchChange() {
        Long firmId = headerUtil.getHeaderFirmId();
        List<AlarmEventDTO> results = eventService.searchChange(firmId);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    @ApiOperation("根据电站ID和告警等级代码获得当日正在告警")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "电站Id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmLevelId", value = "告警等级ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/findByStationAndLevelId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findWarningDayByStationAndAlarmLevel(@RequestParam("stationId") Long stationId,
                                                                             @RequestParam("alarmLevelId") Long alarmLevelId) throws ArgumentErrorException {
        List<AlarmEventSearchDTO> results = eventService.findWarningDayByStationAndAlarmLevel(stationId, alarmLevelId);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    @ApiOperation("根据电站ID和告警事件代码获得当日事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "电站Id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "alarmTypeId", value = "告警类型Id", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/findByStationAndTypeId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findWarningDayByStationAndTypeName(@RequestParam("stationId") Long stationId,
                                                                           @RequestParam("alarmTypeId") Long alarmTypeId) throws ArgumentErrorException {
        List<AlarmEventSearchDTO> results = eventService.findEventDayByStationIdAndTypeName(stationId, alarmTypeId);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    @ApiOperation("根据电站ID获得未消除的事件数量（按照告警等级排列）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationIds", value = "电站ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/searchRealTimeCount", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchRealTimeCount(@RequestParam("stationIds") List<Long> stationIds) {
        Map<String, Integer> result = eventService.realTimeWarningCountByStationIds(stationIds);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据设备ID和告警状态ID消除告警事件记录")
    @RequestMapping(value = "/clearEvent", method = RequestMethod.PUT)
    public ResponseEntity<ResultObject>  clearEventNotEliminated(@RequestBody Map<Long, List<Long>> deviceStatusMap) throws UniqueConstraintsException, ObjectNotFoundException, ArgumentErrorException {
        eventService.clearEventNotEliminated(deviceStatusMap);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }
//
//    @ApiOperation("根据设备ID和告警状态ID消除告警事件记录")
//    @RequestMapping(value = "/fromDpu", method = RequestMethod.POST)
//    public ResponseEntity<ResultObject> createEventFromDpu(@RequestBody List<AlarmEventDpuDTO> alarmEventDpuDTOList) {
//        eventService.createEventFromDpu(alarmEventDpuDTOList);
//        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
//    }
}